<?php
/**
 * Created by PhpStorm.
 * User: yxb
 * Date: 2016/12/22
 * Time: 19:22
 */
namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Url;
class Detail extends Controller{
    /**
    商品属性
     * @param g_id 商品id
     * @param uid
     * @param assess_token
     */
    public function attribute($g_id = 0,$uid = 0,$assess_token = ''){
        empty($g_id) && $this->error("参数错误");
        $where_goods = [
            'is_delete' => 0,
            'id' => $g_id,
            'status' => 1
        ];
        $field = [
            'id',
            'subject',
            'introduction',
            'price',
            'integral',
            'sell_num',
            's_id',
            'comment_num',
            'num'
        ];
        $goods = Db::name("goods")->where($where_goods)->field($field)->find();
        if(empty($goods)){
            return format_return(0,'','商品已下架');
        }else{
            $where_attribute = [
                'is_delete' => 0,
                'g_id' => $g_id
            ];
            $field_attr = [
                'id',
                'atr_id',
                'attribute'
            ];
            $field_ass = [
                'id',
                'atr_id',
                'a_id',
                'assessment'
            ];
            $attribute = Db::name("goods_assessment")->where($where_attribute)->field($field_attr)->group("atr_id")->select();
            if(!empty($attribute)){
                $ids = array_column($attribute,'atr_id');
                unset($attribute);
                $attribute = Db::name("attribute")->where('id','in',$ids)->where("is_delete",0)->order("is_color desc")->select();
                $assessment = Db::name("goods_assessment")->where($where_attribute)->field($field_ass)->select();
                foreach($attribute as $key => $value){
                    for($i = 0; $i < sizeof($assessment) ;$i ++){
                        if($value['id'] == $assessment[$i]['atr_id']){
                            $attribute[$key]['assessment'][] = $assessment[$i];
                        }
                    }
                }
            }else{
                $attribute = '';
            }
            $where_pic = [
                'g_id' => $g_id,
                'type' => 0,
                'is_delete' => 0
            ];
            $field_pic = [
                'img',
                'id'
            ];
            $picture = Db::name("goods_picture")->where($where_pic)->field($field_pic)->limit(4)->select();
            $data['collection'] = 0;
            if(!empty($uid)){
                if(is_token($uid,$assess_token) != 0){
                    $where_collection = [
                        'uid' => $uid,
                        'g_id' => $g_id,
                        'is_delete' => 0
                    ];
                    $collection = Db::name("collection")->where($where_collection)->find();
                    if(!empty($collection)){
                        $data['collection'] = 1;
                    }
                }
            }
            $where_store = [
                'id' => $goods['s_id'],
                'status' => 1
            ];
            $field_store = [
                'name',
                'uid',
                'id'
            ];
            $store_online = Db::name('store_online')->where($where_store)->field($field_store)->find();
            $member = Db::name("members")->where('uid',$store_online['uid'])->find();
            $store_online['logo'] = $member['logo'];
            $data['picture'] = $picture;
            $data['goods'] = $goods;
            $data['attribute'] = $attribute;
            $data['store'] = $store_online;
            return format_return(1,$data,"数据获取成功");
        }
    }

    /**
    商品详情
     * @param g_id 商品id
     */
    public function detail($g_id = 0){
        empty($g_id) && $this->error("参数错误");
        $where_goods = [
            'is_delete' => 0,
            'id' => $g_id,
            'status' => 1
        ];
        $field = [
            'id',
            'detail',
        ];
        $goods = Db::name("goods")->where($where_goods)->field($field)->find();
        if(empty($goods)){
            return format_return(0,'','商品已下架');
        }else{
            return format_return(1,$goods,"数据获取成功");
        }
    }

    /**
    商品评价
     * @param g_id 商品id
     * @param page 分页id
     */
    public function comment($g_id = 0,$page = 0){
        empty($g_id) && $this->error("参数错误");
        $start = $page*10;
        $where_goods = [
            'is_delete' => 0,
            'id' => $g_id,
            'status' => 1
        ];
        $goods = Db::name("goods")->where($where_goods)->find();
        if(empty($goods)){
            return format_return(0,'','商品已下架');
        }else{
            $where_comment = [
                'g_id' => $g_id,
                'is_delete' => 0
            ];
            $comment = Db::name("goods_comment")->where($where_comment)->limit($start,10)->select();
            $where_picture = [
                'is_delete' => 0,
                'g_id' => $g_id
            ];
            foreach($comment as $key => $value){
                $member = Db::name("members")->where("uid",$comment[$key]['uid'])->find();
                $comment[$key]['logo'] = $member['logo'];
                $comment[$key]['name'] = $member['username'];
                $comment[$key]['timestamp'] = date('Y-m-d H:i:s',$value['timestamp']);
                $where_picture['c_id'] = $value['id'];
                $comment[$key]['picture'] = Db::name("comment_picture")->where($where_picture)->limit(4)->select();
            }
            return format_return(1,$comment,'数据获取成功');
        }
    }

        //调用查询物流轨迹
        // @param order_id
        //---------------------------------------------
        public function get_logistics($order_id = ''){
            $order = Db::name("orders")->where("order_id",$order_id)->find();
            $where['name'] = ['like','%'.$order['courier'].'%'];
            $code = Db::name("express")->where($where)->find();
            //dump($code);die;

            if(!empty($code)){
                $logisticResult=getOrderTracesByJson($code['code'],$order['courier_no']);
                $obj = json_decode($logisticResult);
                $obj->courier = $order['courier'];
                //return json_decode($logisticResult);
                return format_return(1,$obj,"数据获取成功");
            }else{
                return format_return(0,"","暂时不支持此快递");
            }
        }
}